package cn.edu.zjicm.wordsnet_d.synchronize;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import cn.edu.zjicm.wordsnet_d.data.School;
import cn.edu.zjicm.wordsnet_d.download.JudgeNetwork;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import android.content.Context;
import android.os.Handler;

public class SchoolInterface extends ServerInterface {

	private static SchoolInterface mInstance;

	public static SchoolInterface getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new SchoolInterface(context);
		}
		return mInstance;
	}

	private SchoolInterface(Context context) {
		super(context);
	}

	public void getSchoolList(int schoolLevel, int locationId, Handler handler) {
		if (JudgeNetwork.getInstance(context).isWifiConnected() || JudgeNetwork.getInstance(context).isMobileConnected()) {
			new GetSchoolListThread(schoolLevel, locationId, handler).start();
		} else {
			// Toast.makeText(context, "请确认网络是否连接", Toast.LENGTH_LONG).show();
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}
	}

	public class GetSchoolListThread extends Thread {
		int schoolLevel;
		int locationId;
		Handler handler;

		public GetSchoolListThread(int schoolLevel, int locationId, Handler handler) {
			this.schoolLevel = schoolLevel;
			this.locationId = locationId;
			this.handler = handler;
		}

		@Override
		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			UserInterface.setClientTimeout(client);

			String url = null;
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = null;
			if (schoolLevel == 1) {
				pair1 = new BasicNameValuePair("pid", locationId + "");
				// pair1 = new BasicNameValuePair("pid",
				// DefaultTripleDES.getInstance().encrypt(locationId + ""));
				url = GET_SCHOOL1;
			} else if (schoolLevel == 2) {
				pair1 = new BasicNameValuePair("rid", locationId + "");
				url = GET_SCHOOL2;
			} else if (schoolLevel == 3) {
				pair1 = new BasicNameValuePair("rid", locationId + "");
				url = GET_SCHOOL3;
			} else if (schoolLevel == 4) {
				pair1 = new BasicNameValuePair("rid", locationId + "");
				url = GET_SCHOOL4;
			}
			list.add(pair1);

			try {
				HttpEntity entity = new UrlEncodedFormEntity(list);
				HttpPost post = new HttpPost(url);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");

					if (success) {
						String data = result.getString("schoollist");
						Gson gson = new Gson();
						List<School> schoolList = gson.fromJson(data, new TypeToken<List<School>>() {
						}.getType());
						// LogUtil.e("school list", schoolList.toString());
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS, schoolList));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_UNSUCCESS));
					}
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
			}
		}
	}
}
